package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmchannelMapper;
import com.heima.wemedia.service.WmchannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class WmchannelServiceImpl extends ServiceImpl<WmchannelMapper, WmChannel> implements WmchannelService {

   /*
   * 查询文章列表
   *
   * */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /*
    * 删除频道
    *
    * */
    @Override
    public ResponseResult delete(Integer id) {
       if(id==null){
           return ResponseResult.errorResult(500,"参数不能为空");
       }
       removeById(id);
        return ResponseResult.okResult(0, "删除成功");


    }

    /*
    * 聊表查询
    * */
    @Override
    public ResponseResult list(ChannelDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(500,"参数不能为空");
        }
        //分页检测
        dto.checkParam();

        //模糊查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(objectLambdaQueryWrapper!=null){
            objectLambdaQueryWrapper.like(WmChannel::getName,dto.getName());
        }
        page(page,objectLambdaQueryWrapper);
        ResponseResult responseResult = new ResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;


    }

    /*
    * 保存
    * */
    @Override
    public ResponseResult save1(WmChannel wmChannel) {
        //参数校验
        if(wmChannel==null){
            return ResponseResult.errorResult(500,"参数不能为空");
        }

        //已经存在的频道不能保存
        WmChannel wmChannel1 = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if(wmChannel1!=null){
            return ResponseResult.errorResult(500,"频道名称已经存在");
        }

        //保存
        wmChannel1.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(0,"保存成功");

    }

    /*
    * 修改频道
    *
    * */
    @Override
    public Object update1(WmChannel wmChannel) {
       if (wmChannel==null){
           return ResponseResult.errorResult(500,"参数不能为空");
       }
       removeById(wmChannel);
       return ResponseResult.okResult(0,"修改成功");
    }
}
